package com.gzdh.dishrecognition.ui.adapter.paicai;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alipay.iot.bpaas.api.utils.StringUtil;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.model.HosMenuDto;
import com.gzdh.dishrecognition.model.NWeekMenuBakDto;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RightMenuAdapter extends RecyclerView.Adapter<RightMenuAdapter.ViewHolder> {

    private final Context mContext;

    private int select = -1;

    List<NWeekMenuBakDto> list = new ArrayList<>();

    List<NWeekMenuBakDto> oldList;

    private String dinnerBm;

    private String zl;


    private OnUserResult onUserResult;

    public void setOnUserResult(OnUserResult onUserResult) {
        this.onUserResult = onUserResult;
    }

    public void setZl(String zl) {
        this.zl = zl;
    }

    public void setDinnerBm(String dinnerBm) {
        this.dinnerBm = dinnerBm;
    }

    public RightMenuAdapter(Context context) {
        this.mContext = context;
        this.list = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_food, parent, false);
        return new ViewHolder(itemView);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.nameTv.setText(list.get(position).getName());
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadius(10);
        drawable.setColor(Color.rgb(250, 250, 250));
        holder.mainLinear.setBackground(drawable);

        holder.mainLinear.setOnClickListener(view -> {
            NWeekMenuBakDto nWeekMenuBakDto2 = list.get(position);
            Stream<NWeekMenuBakDto> nWeekMenuBakDtoStream = oldList.stream();
            nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> !nWeekMenuBakDto.isDelete());
            nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getDinner().equals(dinnerBm));
            nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getZl().equals(zl));
            nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getBm().equals(nWeekMenuBakDto2.getBm()));
            nWeekMenuBakDtoStream.findFirst().get().setDelete(true);
            showNotify();
        });
        holder.price.setText(list.get(position).getPrice());
    }

    public void addMenu(HosMenuDto hosMenuDto) {

        Stream<NWeekMenuBakDto> nWeekMenuBakDtoStream = oldList.stream();
        nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> !nWeekMenuBakDto.isDelete());
        nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getDinner().equals(dinnerBm));
        nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getZl().equals(zl));
        nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getBm().equals(hosMenuDto.getBm()));
        NWeekMenuBakDto nWeekMenuBakDto = nWeekMenuBakDtoStream.findFirst().orElse(null);
        if (nWeekMenuBakDto != null) {
            nWeekMenuBakDto.setDelete(false);
        } else {
            nWeekMenuBakDto = new NWeekMenuBakDto();
            nWeekMenuBakDto.setDay("");
            nWeekMenuBakDto.setZl(zl);
            nWeekMenuBakDto.setDinner(dinnerBm);
            nWeekMenuBakDto.setBm(hosMenuDto.getBm());
            nWeekMenuBakDto.setName(hosMenuDto.getName());
            nWeekMenuBakDto.setMcjc(hosMenuDto.getMcjc());
            nWeekMenuBakDto.setPrice(hosMenuDto.getPrice());
            nWeekMenuBakDto.setSavedm("");
            oldList.add(nWeekMenuBakDto);
        }
        showNotify();
    }

    public void setList(List<NWeekMenuBakDto> list) {
        this.list = list;
        this.oldList = list;
    }


    public void showNotify() {
        if (oldList == null || oldList.size() == 0) {
            return;
        }
        Stream<NWeekMenuBakDto> nWeekMenuBakDtoStream = oldList.stream();
        nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> !nWeekMenuBakDto.isDelete());
        if (!StringUtil.isEmpty(dinnerBm)) {
            nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getDinner().equals(dinnerBm));
        }
        if (!StringUtil.isEmpty(zl)) {
            nWeekMenuBakDtoStream = nWeekMenuBakDtoStream.filter(nWeekMenuBakDto -> nWeekMenuBakDto.getZl().equals(zl));
        }
        List<NWeekMenuBakDto> nWeekMenuBakDtoList = nWeekMenuBakDtoStream.collect(Collectors.toList());
        if (onUserResult != null) {
            onUserResult.onUserClick(nWeekMenuBakDtoList);
        }

        list = nWeekMenuBakDtoList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView nameTv;

        TextView price;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.food_main);
            nameTv = item.findViewById(R.id.tv_name);
            price = item.findViewById(R.id.tv_price);
        }
    }

    public interface OnUserResult {
        public void onUserClick(List<NWeekMenuBakDto> nWeekMenuBakDtoList);
    }

    public List<NWeekMenuBakDto> submitWeekMenu() {
        if (oldList == null) {
            return null;
        }
        List<NWeekMenuBakDto> weekMenuBakDtos = oldList.stream().filter(nWeekMenuBakDto ->
                        nWeekMenuBakDto.isDelete() || StringUtil.isEmpty(nWeekMenuBakDto.getPkid()))
                .collect(Collectors.toList());

        return weekMenuBakDtos;
    }
}
